package com.dropdb.demo.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 解决前后端分离session跨域问题
 */
public class CustomWebSessionManager extends DefaultWebSessionManager {
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;
        String header = req.getHeader("X-cskaoyan-mall-Admin-Token");
        // 如果有session的header，使用该session
        if (!StringUtils.isEmpty(header)) {
            return header;
        }
        return super.getSessionId(request, response);
    }
}
